package models

import (
	"gorm.io/gorm"
)

type Store struct {
	gorm.Model
	Name     string `gorm:"type:varchar(30);not null"` //店铺名称
	Types    string `gorm:"type:varchar(20);"`         //店铺类型
	UserName string `gorm:"type:varchar(20);not null"` //注册人
	Tel      string `gorm:"type:char(11);"`            //电话号
	Status   string `gorm:"type:varchar(20);"`         //店铺认证状态
}

func (s *Store) Create() error {
	return DB.Create(&s).Error
}

func (s *Store) List(page, pageSize int) (list []Store, err error) {
	err = DB.Scopes(Paginate(page, pageSize)).Find(&list).Error
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
func (s *Store) GetNameById() error {
	return DB.Where("id=?", s.ID).Find(&s).Error
}

func (s *Store) Update() error {
	return DB.Updates(&s).Error
}
